Skip to content

refactor(queries): extract shared bucket-pagination helper for get_ca… - #717

Merged
davidmaronio merged 4 commits into
Iris-IV:mainfrom
Nife-tanny:refactor/shared_bucket_pagination
Aug 6, 2026
Merged

refactor(queries): extract shared bucket-pagination helper for get_ca…#717
davidmaronio merged 4 commits into
Iris-IV:mainfrom
Nife-tanny:refactor/shared_bucket_pagination

Conversation

@Nife-tanny

@Nife-tanny Nife-tanny commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Extract the identical bucket-traversal algorithm previously duplicated in get_campaigns_by_category and get_creator_campaigns into a single private helper, parameterised by total count, bucket size, and a bucket-getter closure.

Update after review: the extraction itself has since landed on main via #734 (as get_campaigns_from_buckets, covering both callers). This PR's incremental contribution is now:

  • the algorithm documentation for the shared helper, and
  • a code comment pinning the intentional if let Some sparse-bucket behaviour.

Algorithm (unchanged)

  1. Jump to the bucket containing the page offset.
  2. Walk entries within that bucket starting at the requested position.
  3. Collect up to limit campaigns (capped at LIST_MAX_LIMIT).
  4. When the bucket is exhausted, advance position past the bucket boundary and repeat from step 1 with the next bucket.

Behaviour notes (reviewer follow-ups)

  • Intentional: the category path uses if let Some(campaign_id) = bucket.get(idx_in_bucket) (the safer form the creator path already used) rather than unwrap(). A sparse bucket — one whose entry is absent — now skips the slot instead of panicking. This is deliberate so both paths behave identically.
  • get_campaigns_by_category now reads the category total before the limit == 0 early return — one extra storage read on a degenerate call, understood and accepted.

Validation

  • Public function signatures unchanged — backwards-compatible.
  • Only src/queries.rs modified.
  • cargo fmt --check and cargo clippy --all-targets --features testutils -- -D warnings pass cleanly.
  • Full suite: 421 tests pass, including the rebased 4-tuple test_campaign_update payload assertions.

Closes #663

…mpaigns_by_category and get_creator_campaigns (Iris-IV#663)

Introduce a private  helper that encapsulates
the identical bucket-traversal algorithm previously duplicated in
 and . The helper is
parameterised by:
  - total count (derived by each caller from its own domain counter)
  - bucket size (CATEGORY_CAMPAIGNS_BUCKET_SIZE / CREATOR_CAMPAIGNS_BUCKET_SIZE)
  - a bucket getter closure (each caller supplies its own storage lookup)

Algorithm (unchanged):
  1. Jump to the bucket containing the page offset.
  2. Walk entries within that bucket starting at the requested position.
  3. Collect up to  campaigns (capped at LIST_MAX_LIMIT).
  4. When the bucket is exhausted, advance position past the bucket
     boundary and repeat from step 1 with the next bucket.

Behaviour preservation:
  - Public function signatures unchanged — backwards-compatible.
  - Only src/queries.rs modified.
  - All 400 workspace tests run; only 2 pre-existing failures remain
    (test_campaign_update — unrelated Soroban host vector unpacking).
  - 26 query/bucket/benchmark tests pass with zero regressions.
  - cargo fmt --check and cargo clippy --all-targets --features testutils
    both pass cleanly.

Closes Iris-IV#663
@drips-wave

drips-wave Bot commented Jul 30, 2026

Copy link
Copy Markdown

@Nife-tanny Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@davidmaronio

Copy link
Copy Markdown
Contributor

good refactor. i diffed the extracted paginate_bucketed_campaigns against both original loops and the traversal logic (bucket jump, in-bucket offset, empty-bucket skip, bucket_len advance) is faithfully preserved, and the closure-per-caller design keeps the storage access patterns identical. the algorithm doc comment is a nice touch.

two small notes, neither blocking:

  1. src/queries.rs: the category path quietly changed from bucket.get(idx_in_bucket).unwrap() to the if let Some(campaign_id) form the creator path used. that's the safer of the two, but it is a subtle behavior change (a sparse bucket now skips instead of panicking), so worth a line in the PR description saying it's intentional.

  2. the early-return ordering changed for get_campaigns_by_category: previously limit == 0 returned before computing total; now total is always read first. one extra storage read on a degenerate call, fine, just noting it's understood.

on CI: the two failing tests are test_campaign_update payload assertions that main already fixed (4-tuple event), your branch just predates that. not this PR's fault.

gate: resolve the conflicts with main (branch shows DIRTY), CI should go green on its own after that, then this is good to merge.

Nife-tanny and others added 3 commits August 4, 2026 11:10
)

The extraction itself already landed on main via Iris-IV#734; this PR's remaining contribution is the algorithm documentation for the shared helper, plus a code comment pinning the intentional `if let Some` sparse-bucket behavior (a sparse bucket skips instead of panicking - the safer of the two forms, adopted on purpose).

@davidmaronio davidmaronio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both notes addressed, the doc comment on the helper and the if-let rationale read well. extraction is still faithful after the rebase. merging.

@davidmaronio
davidmaronio merged commit c50c40c into Iris-IV:main Aug 6, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Refactor] Extract a shared bucketed-pagination helper for queries.rs

2 participants